package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.SystemConfig;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.entity.tree.Node;
import cc.yugu.yhdw.common.entity.tree.NodeModel;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.SystemConfigMapper;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
 * 系统配置业务
 * 
 * @Date:Created in 2018/7/5 13:37
 * @Author:
 * @Modified By: zjn 2018/7/5 13:37
 */ 
@Service
public class SystemConfigService {


    @Autowired
    SystemConfigMapper systemConfigMapper;
    @Autowired
    AppConfig appConfig;
    /**
    * 查询表 bas_system_config
    * 
    * @Param: []
    * @Return: java.util.List<cc.yugu.yhdw.common.domain.basic.SystemConfig>
    * @Author By: zjn 2018/7/5  13:37
    */ 
    public List<SystemConfig> findAll() {
        SystemConfig search = new SystemConfig();
        //按照 系统配置参数 查询
        return systemConfigMapper.findAll(search);
    }
    /**
    * 按主键查询
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.SystemConfig
    * @Author By: zjn 2018/7/5  13:44
    */
    public SystemConfig find(String id){
        return systemConfigMapper.find(id);
    }
    /**
    * 分页
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/5  13:45
    */ 
    public Page findPage(SystemConfig search) {
        Page page = search.buildPage();
        page.setResult(systemConfigMapper.findAll(search));
        return page;
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: void
    * @Author By: zjn 2018/7/5  13:45
    */ 
    public void update(SystemConfig entity) {
        // 如果获取的名称不为null的话，去除名称字符首尾的空格
        entity.setConfigValue(StringUtils.trimToEmpty(entity.getConfigValue()));
        systemConfigMapper.update(entity);
    }

    /**
    * 初始化系统配置
    * 
    * @Param: []
    * @Return: void
    * @Author By: zjn 2018/7/5  13:50
    */ 
    public void initConfig() {
        // 按照系统配置参数查询
        List<SystemConfig> list = findAll();
        Map<String, SystemConfig> map = new HashMap<String, SystemConfig>();

        for(SystemConfig e : list) {
            // 键值对存储
            map.put(e.getId(), e);
        }
        // 根据key（"static.url"） 获取值，在获取 名称（静态地址），在保存到对象中
        appConfig.setStaticUrl((String) map.get(ConstEnum.SystemConfigKey.STATIC_URL.getValue()).getConfigValue());
    }

    /**
    * 输出目录树
    * 
    * @Param: [dummy, stream]
    * @Return: void
    * @Author By: zjn 2018/7/5  13:58
    */ 
    public void tree(String dummy, OutputStream stream) throws IOException {
        // 根据 显示标识 查询分类名称 和分类类型，并按照分类类型有序排列
        List<SystemConfig> list =  systemConfigMapper.findAllCategory();
        List<Node<NodeModel>> roots = new ArrayList<Node<NodeModel>>();
        if(StringUtils.isNotEmpty(dummy)) {
            NodeModel data = new NodeModel();
            Node<NodeModel> root = new Node<NodeModel>(data);
            data.setId(0);
            data.setName(dummy);
            roots.add(root);

            for(SystemConfig s : list) {
                NodeModel nodeModel = new NodeModel();
                Node<NodeModel> node = new Node<NodeModel>(nodeModel);
                nodeModel.setId(s.getCategoryType());
                nodeModel.setName(s.getCategoryName());
                root.addChild(node);
            }
        } else {
            for(SystemConfig s : list) {
                NodeModel nodeModel = new NodeModel();
                Node<NodeModel> node = new Node<NodeModel>(nodeModel);
                nodeModel.setId(s.getCategoryType());
                nodeModel.setName(s.getCategoryName());
                roots.add(node);
            }
        }

        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator json = objectMapper.getJsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
        json.writeStartArray();
        YhdwUtils.writeJson(roots, json);
        json.writeEndArray();
        json.flush();
        json.close();
    }

}
